#!/bin/bash
set -e

source $(dirname $0)/version

cd $(dirname $0)/..

mkdir -p bin
mkdir -p dist/artifacts

LINKFLAGS="-X github.com/rancher/fleet/pkg/version.Version=$VERSION"
LINKFLAGS="-X github.com/rancher/fleet/pkg/version.GitCommit=$COMMIT $LINKFLAGS"
LINKFLAGS="-s -w $LINKFLAGS"
LINKFLAGS="$LINKFLAGS -extldflags -static"

AGENT_ARCH=amd64

echo "Building binary: fleet"
GOOS=linux GOARCH=$AGENT_ARCH CGO_ENABLED=0 go build -ldflags "$LINKFLAGS" -o bin/fleet-linux-$AGENT_ARCH ./cmd/fleetcli
cp bin/fleet-linux-$AGENT_ARCH dist/artifacts/

echo "Building binary: fleetagent"
GOOS=linux GOARCH=$AGENT_ARCH CGO_ENABLED=0 go build -ldflags "$LINKFLAGS" -o bin/fleetagent-linux-$AGENT_ARCH ./cmd/fleetagent
cp bin/fleetagent-linux-$AGENT_ARCH dist/artifacts/

IMAGE=$REPO/fleet-agent:$TAG
echo "Building fleet-agent image for linux-$AGENT_ARCH: $IMAGE"
docker build -f package/Dockerfile.agent -t $IMAGE --build-arg ARCH=$AGENT_ARCH .
